﻿using BLL;
using Model;
using System;
using System.Data;
using System.Text;
using System.Web.UI.WebControls;

namespace Thksoft.Modal
{
    public partial class EditQueue : BaseUI.BasePage
    {
        private Lc_QueueBLL queueBLL = new Lc_QueueBLL();
        private Lc_ProcessBLL processBLL = new Lc_ProcessBLL();
        private Lc_NodeBLL nodeBLL = new Lc_NodeBLL();

        private Sys_UserBLL userBLL = new Sys_UserBLL();
        private Lc_BlockBLL blockBLL = new Lc_BlockBLL();

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                FORM_KEY.Value = Request.QueryString["ID"];
                FORM_IFEDIT.Value = Request.QueryString["IF_EDIT"];
                Bind();
            }
        }

        /// <summary>
        /// 绑定数据
        /// </summary>
        private void Bind()
        {
            #region 绑定流程节点

            DataTable table = processBLL.GetProcByState("AND N_PROCESS_STATE = 1", CurrentUserName);
            FORM_PROCESS.DataSource = table;
            FORM_PROCESS.DataTextField = "C_PROCESS_NAME";
            FORM_PROCESS.DataValueField = "N_PROCESS_ID";
            FORM_PROCESS.DataBind();
            FORM_PROCESS.Items.Insert(0, new ListItem("---请选择---", "-1"));

            #endregion 绑定流程节点

            if (FORM_IFEDIT.Value == "TRUE")
            {
                //绑定要修改的值
                Lc_Queue model = queueBLL.Find(Convert.ToDecimal(FORM_KEY.Value));
                try
                {
                    FORM_PROCESS.SelectedValue = model.N_PROCESS_ID.ToString();
                }
                catch (Exception)
                {
                    FORM_PROCESS.SelectedValue = "-1";
                }
                //绑定节点信息
                var dtNode = nodeBLL.GetAllInfo("ProcessID", FORM_PROCESS.SelectedValue);
                if (dtNode != null && dtNode.Rows.Count > 0)
                {
                    FORM_NODE.DataSource = dtNode;
                    FORM_NODE.DataTextField = "C_NODE_NAME";
                    FORM_NODE.DataValueField = "N_NODE_ID";
                    FORM_NODE.DataBind();
                    FORM_NODE.Items.Insert(0, new ListItem("---请选择---", "-1"));
                }
                try
                {
                    FORM_NODE.SelectedValue = model.N_NODE_ID.ToString();
                }
                catch (Exception)
                {
                    FORM_NODE.SelectedValue = "-1";
                }
                FORM_AUDIT_TYPE.SelectedValue = model.N_AUDIT_TYPE.ToString();
                FORM_NUMBER.Value = model.N_QUEUE_NUMBER.ToString();
                FORM_USER_STRING.Value = string.IsNullOrEmpty(model.C_QUEUE_TRANSACTION_ID) ? "" : GetUserNames(model.C_QUEUE_TRANSACTION_ID);
                FORM_USER.Value = model.C_QUEUE_TRANSACTION_ID == null ? "" : model.C_QUEUE_TRANSACTION_ID;
                FORM_BLOCK.Value = model.C_QUEUE_BLOCK == null ? "" : model.C_QUEUE_BLOCK;
                DataTable dtBlock = blockBLL.GetAllInfo("BlockID", model.C_QUEUE_BLOCK.ToString());
                var tblock = "";
                if (dtBlock != null && dtBlock.Rows.Count > 0)
                {
                    foreach (DataRow dr in dtBlock.Rows)
                    {
                        tblock += dr["C_BLOCK_NAME"].ToString() + ",";
                    }
                    tblock = tblock.Substring(0, tblock.Length - 1);
                }
                FORM_BLOCK_STRING.Value = tblock == "" ? "无" : tblock;
                FORM_GY_TABLE.Text = model.C_SPICAL_TABLE.ToString();
                FORM_GY_USER.Text = model.C_SPICAL_COL.ToString();
                FORM_ALLOW_ZX.Checked = model.N_CANCEL == 1 ? true : false;
                FORM_SPICAL_TRAN.Checked = model.N_CHANGE == 1 ? true : false;
            }
            else
            {
            }
        }

        protected void btnSave_Click(object sender, EventArgs e)
        {
            if (FORM_PROCESS.SelectedValue == "-1")
            {
                MessageBox.Show(Page, "请选择流程");
                return;
            }

            if (FORM_NODE.SelectedValue == "0")
            {
                MessageBox.Show(Page, "请选择节点");
                return;
            }

            if (FORM_AUDIT_TYPE.SelectedValue == "-1")
            {
                MessageBox.Show(Page, "请选择办理方式");
                return;
            }
            if (FORM_AUDIT_TYPE.SelectedValue != "5")
            {
                MessageBox.Show(Page, "请选择办理人员");
                return;
            }

            #region 新增

            {
                Lc_Queue model = new Lc_Queue
                {
                    N_PROCESS_ID = decimal.Parse(FORM_PROCESS.SelectedValue),
                    N_NODE_ID = decimal.Parse(FORM_NODE.SelectedValue),
                    N_QUEUE_NUMBER = decimal.Parse(FORM_NUMBER.Value),
                    C_QUEUE_TRANSACTION_ID = FORM_USER.Value,
                    N_AUDIT_TYPE = decimal.Parse(FORM_AUDIT_TYPE.SelectedValue),
                    C_QUEUE_BLOCK = FORM_BLOCK.Value,
                    C_SPICAL_TABLE = FORM_GY_TABLE.Text,
                    C_SPICAL_COL = FORM_GY_USER.Text,
                    N_CANCEL = FORM_ALLOW_ZX.Checked ? 1 : 0,
                    N_CHANGE = FORM_SPICAL_TRAN.Checked ? 1 : 0
                };
                if (queueBLL.Insert(model, CurrentUserName))
                {
                    FORM_IFEDIT.Value = "TRUE";
                    FORM_KEY.Value = model.N_QUEUE_ID.ToString();
                    var Builder = new StringBuilder();
                    Builder.AppendLine("   var option={");
                    Builder.AppendLine("                Msg:\"【温馨提示】添加成功！\",");
                    Builder.AppendLine("                YesCallBack:function(index){");
                    Builder.AppendLine("                                FORM_UPDATE_TITLE(\"编辑队列\");");
                    Builder.AppendLine("                }");
                    Builder.AppendLine("   };");
                    Builder.AppendLine("   top.MessageBox.Alert(option);");
                    MessageBox.ResponseScript(this, Builder.ToString());
                }
                else
                {
                    MessageBox.Show(Page, "新增失败");
                }
            }

            #endregion 新增

            #region 修改

            {
                //根据队列编号查询队列信息
                Lc_Queue model = queueBLL.Find(Convert.ToDecimal(FORM_KEY.Value));// .GetInfoById(Request.QueryString["ID"].ToString());
                model.N_PROCESS_ID = decimal.Parse(FORM_PROCESS.SelectedValue);
                model.N_NODE_ID = decimal.Parse(FORM_NODE.SelectedValue);
                model.N_QUEUE_NUMBER = decimal.Parse(FORM_NUMBER.Value);
                model.C_QUEUE_TRANSACTION_ID = FORM_USER.Value;//HIds.Value;
                model.N_AUDIT_TYPE = decimal.Parse(FORM_AUDIT_TYPE.SelectedValue);
                model.C_QUEUE_BLOCK = FORM_BLOCK.Value;
                model.C_SPICAL_TABLE = FORM_GY_TABLE.Text;
                model.C_SPICAL_COL = FORM_GY_USER.Text;
                model.N_CANCEL = FORM_ALLOW_ZX.Checked ? 1 : 0;
                model.N_CHANGE = FORM_SPICAL_TRAN.Checked ? 1 : 0;

                //判断该流程下该节点对应的队列是否已经存在
                string str = model.N_NODE_ID + ",";
                str += model.N_PROCESS_ID;
                DataTable dt = queueBLL.GetAllInfo("NID", str);

                if (dt.Rows.Count > 0)
                {
                    if (dt.Rows[0]["N_NODE_ID"].ToString() == model.N_PROCESS_ID.ToString())
                    {
                        MessageBox.Show(Page, "该节点对应的队列已经存在，请重新选择");
                        return;
                    }
                }
                if (queueBLL.Update(model, lognUser.USER_NAME))
                {
                    MessageBox.Show(Page, "保存成功");
                    return;
                }
                else
                {
                    MessageBox.Show(Page, "保存失败");
                    return;
                }
            }

            #endregion 修改
        }

        protected void btnReturn_Click(object sender, EventArgs e)
        {
            Response.Redirect("/BusinessExpansion/Process/QueueSelect.aspx");
        }

        protected void ddlProcess_SelectedIndexChanged(object sender, EventArgs e)
        {
            //绑定流程信息
            DataTable table = nodeBLL.GetAllInfo("ProcessID", FORM_PROCESS.SelectedValue);
            FORM_NODE.Items.Clear();
            if (table != null && table.Rows.Count > 0)
            {
                FORM_NODE.DataSource = table;
                FORM_NODE.DataTextField = "C_NODE_NAME";
                FORM_NODE.DataValueField = "N_NODE_ID";
                FORM_NODE.DataBind();
                FORM_NODE.Items.Insert(0, new ListItem("---请选择---", "-1"));
                btnSave.Enabled = true;
            }
            else
            {
                MessageBox.Show(Page, "请添加节点信息");
                btnSave.Enabled = false;
                return;
            }
        }
    }
}